package com.canaan.business.service.bill.impl;

import java.util.Date;
import java.util.List;

import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.bill.SalesRefundBillCheckMapper;
import com.canaan.business.domain.bill.SalesRefundBillCheck;
import com.canaan.business.service.bill.ISalesRefundBillCheckService;

/**
 * 销售退货单清单Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-13
 */
@Service
public class SalesRefundBillCheckServiceImpl implements ISalesRefundBillCheckService {
    @Resource
    private SalesRefundBillCheckMapper salesRefundBillCheckMapper;

    /**
     * 查询销售退货单清单
     *
     * @param id 销售退货单清单主键
     * @return 销售退货单清单
     */
    @Override
    public SalesRefundBillCheck selectSalesRefundBillCheckById(Long id) {
        return salesRefundBillCheckMapper.selectSalesRefundBillCheckById(id);
    }

    /**
     * 查询销售退货单清单列表
     *
     * @param salesRefundBillCheck 销售退货单清单
     * @return 销售退货单清单
     */
    @Override
    public List<SalesRefundBillCheck> selectSalesRefundBillCheckList(SalesRefundBillCheck salesRefundBillCheck) {
        return salesRefundBillCheckMapper.selectSalesRefundBillCheckList(salesRefundBillCheck);
    }

    /**
     * 新增销售退货单清单
     *
     * @param salesRefundBillCheck 销售退货单清单
     * @return 结果
     */
    @Override
    public int insertSalesRefundBillCheck(SalesRefundBillCheck salesRefundBillCheck) {
        if(StringUtils.isBlank(salesRefundBillCheck.getNo())){
            salesRefundBillCheck.setNo(TableNoUtil.getNo(SalesRefundBillCheck.class));
        }
        salesRefundBillCheck.setCreateBy(SecurityUtils.getUsername());
        salesRefundBillCheck.setCreateTime(DateUtils.getNowDate());
        return salesRefundBillCheckMapper.insertSalesRefundBillCheck(salesRefundBillCheck);
    }

    /**
     * 批量新增销售退货单清单
     *
     * @param salesRefundBillCheckList 销售退货单清单列表
     * @return 结果
     */
    @Override
    public int batchInsertSalesRefundBillCheck(List<SalesRefundBillCheck> salesRefundBillCheckList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (SalesRefundBillCheck salesRefundBillCheck : salesRefundBillCheckList){
            if(StringUtils.isBlank(salesRefundBillCheck.getNo())){
                salesRefundBillCheck.setNo(TableNoUtil.getNo(SalesRefundBillCheck.class));
            }
            salesRefundBillCheck.setCreateBy(username);
            salesRefundBillCheck.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<salesRefundBillCheckList.size();) {
            int endIndex = i+500;
            if (endIndex > salesRefundBillCheckList.size()) {
                endIndex = salesRefundBillCheckList.size();
            }
            insertNum = insertNum + salesRefundBillCheckMapper.batchInsertSalesRefundBillCheck(salesRefundBillCheckList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改销售退货单清单
     *
     * @param salesRefundBillCheck 销售退货单清单
     * @return 结果
     */
    @Override
    public int updateSalesRefundBillCheck(SalesRefundBillCheck salesRefundBillCheck) {
        salesRefundBillCheck.setUpdateBy(SecurityUtils.getUsername());
        salesRefundBillCheck.setUpdateTime(DateUtils.getNowDate());
        return salesRefundBillCheckMapper.updateSalesRefundBillCheck(salesRefundBillCheck);
    }

    /**
     * 批量修改销售退货单清单
     *
     * @param salesRefundBillCheckList 销售退货单清单列表
     * @return 结果
     */
    @Override
    public int batchUpdateSalesRefundBillCheck(List<SalesRefundBillCheck> salesRefundBillCheckList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (SalesRefundBillCheck salesRefundBillCheck : salesRefundBillCheckList){
            salesRefundBillCheck.setUpdateBy(username);
            salesRefundBillCheck.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<salesRefundBillCheckList.size();) {
            int endIndex = i+500;
            if (endIndex > salesRefundBillCheckList.size()) {
                endIndex = salesRefundBillCheckList.size();
            }
            updateNum = updateNum + salesRefundBillCheckMapper.batchUpdateSalesRefundBillCheck(salesRefundBillCheckList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除销售退货单清单
     *
     * @param ids 需要删除的销售退货单清单主键集合
     * @return 结果
     */
    @Override
    public int deleteSalesRefundBillCheckByIds(Long[] ids) {
        return salesRefundBillCheckMapper.deleteSalesRefundBillCheckByIds(ids);
    }

    /**
     * 删除销售退货单清单信息
     *
     * @param id 销售退货单清单主键
     * @return 结果
     */
    @Override
    public int deleteSalesRefundBillCheckById(Long id) {
        return salesRefundBillCheckMapper.deleteSalesRefundBillCheckById(id);
    }

    /**
     * 查询销售退货单清单
     *
     * @param no 编号
     * @return 销售退货单清单
     */
    @Override
    public SalesRefundBillCheck selectSalesRefundBillCheckByNo(String no) {
        return salesRefundBillCheckMapper.selectSalesRefundBillCheckByNo(no);
    }

    /**
     * 批量删除销售退货单清单
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteSalesRefundBillCheckByNos(String[] nos) {
        return salesRefundBillCheckMapper.deleteSalesRefundBillCheckByNos(nos);
    }

    /**
     * 删除销售退货单清单信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteSalesRefundBillCheckByNo(String no) {
        return salesRefundBillCheckMapper.deleteSalesRefundBillCheckByNo(no);
    }
}
